In Python, you can use the input() function to get user input. The input() function reads a line from input (usually user input), converts the input into a string, and returns it.
Here’s a simple example:
python input from user example - user input example
name = input("Please enter your name: ")
print("Hello, " + name + "!")
Output
Please enter your name:
Sathish
Hello, Sathish!
In this example, the input() function displays the string "Please enter your name: " as a prompt to the user. The user can then type their name and press Enter. The text they entered is then stored in the name variable as a string.
It’s important to note that the input() function always returns a string. If you want to use the input as another data type, you’ll need to convert it using a typecasting function like int(), float(), etc. For example:
Python input from user example with int type conversion
age = int(input("Please enter your age: "))
print("Next year, you will be " + str(age + 1) + ".")
Output
Please enter your age:
20
Next year, you will be 21.
In this example, the input() function gets the user’s age as a string, then the int() function converts that string into an integer. This allows us to perform arithmetic operations with the age variable. When we print the result, we convert the integer back into a string using the str() function.
Remember, if the user enters something that can’t be converted to the desired type (like entering ‘twenty’ when you’re expecting an integer), Python will raise a ValueError. You can use a try/except block to catch this error and handle it gracefully.
here are some more examples of getting user input in Python:
Integer
Python user input example for string
First_Name = str(input("Enter your first name: "))
Last_Name = input("Enter your last name: ")
print("Your name is : ",First_Name+" "+Last_Name)
Output
Enter your first name:
Tutorials
Enter your last name:
Box
Your name is : Tutorials Box
Float
Python input from user example for float type
try:
float_number = float(input("Enter a floating point number: "))
print("You entered: ", float_number)
except ValueError:
print("That's not a valid floating point number!")
Output
Enter a floating point number:
2
You entered: 2.0
List
Python input from user example for List type
try:
list_input = input("Enter a list of numbers separated by space: ")
list_numbers = list(map(int, list_input.split()))
print("You entered: ", list_numbers)
except ValueError:
print("That's not a valid list of numbers!")
Output
Enter a list of numbers separated by space:
1 2 3
You entered: [1, 2, 3]
Boolean
Python input from user example for boolean type
bool_input = input("Enter True or False: ")
if bool_input.lower() == 'true':
bool_value = True
elif bool_input.lower() == 'false':
bool_value = False
else:
print("That's not a valid boolean value!")
print("You entered: ", bool_value)
Output
Enter True or False:
True
You entered: True
Remember, the input() function always returns a string. So, you need to convert the string into the desired data type. If the conversion is not possible (like trying to convert a non-numeric string into an integer), Python will raise a ValueError. You can use a try/except block to catch this error and handle it gracefully.